<template>
  <vue2-org-tree :data="data" :horizontal="true" @on-node-click="onNodeClick" :render-content="renderContent" :current="current" collapsable @on-expand="onExpand" />
</template>
<script>
import Vue from 'vue'
import Vue2OrgTree from 'vue2-org-tree'
Vue.use(Vue2OrgTree)
// import 'vue2-org-tree/dist/style.css'
export default {
  props: {
    current: {
      type: Object,
      default: () => { return new Object() },
    },
    orgData: {
      type: Object,
      default: () => { return new Object() },
    }
  },
  data() {
    return {
      data: {},
    }
  },
  watch: {
    orgData(val) {
      this.data = val;
    },
    current(val) {
      setTimeout(() => {
        var labels = document.getElementsByClassName("tree-label");
        for (var i = 0; i < labels.length; i++) {
          if (labels[i].getAttribute("data-id") == this.current.id) {
            labels[i].classList.add("active");
          } else {
            labels[i].classList.remove("active");
          }
        }
      }, 200);
    }
  },
  methods: {
    onNodeClick(event, data) {
      this.$emit("onNodeClick", data)
    },
    renderContent(h, data) {
      return (<div data-id={data.id} class={'tree-label tree-level-' + data.level}>{data.label}</div>);
    },
    collapse(list) {
      var _this = this;
      list.forEach(function (child) {
        if (child.expand) {
          child.expand = false;
        }
        child.children && _this.collapse(child.children);
      });
    },
    onExpand(e, data) {
      if ("expand" in data) {
        data.expand = !data.expand;
        if (!data.expand && data.children) {
          this.collapse(data.children);
        }
      } else {
        this.$set(data, "expand", true);
      }
    },
  }
};
</script>
<style type="text/css">
.org-tree-container {
  display: table;
  padding: 15px;
  background-color: transparent;
}
.org-tree {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
.org-tree:after,
.org-tree:before {
  content: "";
  display: table;
}
.org-tree:after {
  clear: both;
}
.org-tree-node,
.org-tree-node-children {
  position: relative;
  margin: 0;
  padding: 0;
  list-style-type: none;
}
.org-tree-node-children:after,
.org-tree-node-children:before,
.org-tree-node:after,
.org-tree-node:before {
  -webkit-transition: all 0.35s;
  transition: all 0.35s;
}
.org-tree-node-label {
  position: relative;
  display: inline-block;
}
.org-tree-node-label .org-tree-node-label-inner {
  position: relative;
  /* padding: 10px 15px;
  text-align: center;
  border-radius: 3px;
  -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.15);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.15); */
}
.org-tree-node-label .org-tree-node-label-inner > div {
  padding: 5px 10px;
  text-align: center;
  border-radius: 3px;
  font-size: 0.72vw;
  -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.15);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.15);
  background: linear-gradient(top, #95f6db, #67d3e7 100%);
  background: -o-linear-gradient(top, #95f6db, #67d3e7 100%);
  background: -ms-linear-gradient(top, #95f6db, #67d3e7 100%);
  background: -moz-linear-gradient(top, #95f6db, #67d3e7 100%);
  background: -webkit-linear-gradient(top, #95f6db, #67d3e7 100%);
  color: #000;
  line-height: 20px;
}
.org-tree-node-label .org-tree-node-label-inner > div.active {
  background: linear-gradient(top,  #ffd0a3, #ff9e40 100%);
  background: -o-linear-gradient(top,  #ffd0a3, #ff9e40 100%);
  background: -ms-linear-gradient(top,  #ffd0a3, #ff9e40 100%);
  background: -moz-linear-gradient(top,  #ffd0a3, #ff9e40 100%);
  background: -webkit-linear-gradient(top,  #ffd0a3, #ff9e40 100%);
  font-weight: 600;
  color: #1b1e6c;
}
.org-tree-node-label .org-tree-node-label-inner > div:hover{
  cursor: pointer;
}
.org-tree-node-children .org-tree-node-label-inner::before {
  content: "";
  width: 0;
  height: 0;
  border-top: 4px solid transparent;
  border-left: 8px solid #1cbbbf;
  border-bottom: 4px solid transparent;
  position: absolute;
  left: -8px;
  top: 50%;
  margin-top: -4px;
  z-index: 2;
}
.org-tree-node-btn {
  position: absolute;
  top: 100%;
  left: 50%;
  width: 20px;
  height: 20px;
  z-index: 10;
  margin-left: -11px;
  margin-top: 9px;
  background-color: #169baf;
  border: 1px solid #1699ae;
  border-radius: 50%;
  -webkit-box-shadow: 0 0 2px rgba(0, 0, 0, 0.15);
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  -webkit-transition: all 0.35s ease;
  transition: all 0.35s ease;
}
.org-tree-node-btn:hover {
  background-color: #48cbef;
  -webkit-transform: scale(1.15);
  transform: scale(1.15);
}
.org-tree-node-btn:after,
.org-tree-node-btn:before {
  content: "";
  position: absolute;
}
.org-tree-node-btn:before {
  top: 50%;
  left: 4px;
  right: 4px;
  height: 0;
  border-top: 1px solid #fff;
}
.org-tree-node-btn:after {
  top: 4px;
  left: 50%;
  bottom: 4px;
  width: 0;
  border-left: 1px solid #fff;
}
.org-tree-node-btn.expanded:after {
  border: none;
}
.org-tree-node {
  padding-top: 20px;
  display: table-cell;
  vertical-align: top;
}
.org-tree-node.collapsed,
.org-tree-node.is-leaf {
  padding-left: 10px;
  padding-right: 10px;
}
.org-tree-node:after,
.org-tree-node:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 19px;
}
.org-tree-node:after {
  left: 50%;
  border-left: 1px solid #169db0;
}
.org-tree-node:not(:first-child):before,
.org-tree-node:not(:last-child):after {
  border-top: 1px solid #169db0;
}
.collapsable .org-tree-node.collapsed {
  padding-bottom: 30px;
}
.collapsable .org-tree-node.collapsed .org-tree-node-label:after {
  content: "";
  position: absolute;
  top: 100%;
  left: 0;
  width: 50%;
  height: 20px;
  border-right: 1px solid #169db0;
}
.org-tree > .org-tree-node {
  padding-top: 0;
}
.org-tree > .org-tree-node:after {
  border-left: 0;
}
.org-tree-node-children {
  padding-top: 20px;
  display: table;
}
.org-tree-node-children:before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: 0;
  height: 20px;
  border-left: 1px solid #169db0;
}
.org-tree-node-children:after {
  content: "";
  display: table;
  clear: both;
}
.horizontal .org-tree-node {
  display: inline-block;
  float: none;
  padding-top: 0;
  padding-left: 30px;
}
.horizontal .org-tree-node.collapsed,
.horizontal .org-tree-node.is-leaf {
  padding-top: 8px;
  padding-bottom: 8px;
}
.horizontal .org-tree-node:after,
.horizontal .org-tree-node:before {
  width: 29px;
  height: 50%;
}
.horizontal .org-tree-node:after {
  top: 50%;
  left: 0;
  border-left: 0;
}
.horizontal .org-tree-node:only-child:before {
  top: 0px;
  border-bottom: 1px solid #169db0;
}
.horizontal .org-tree-node:not(:first-child):before,
.horizontal .org-tree-node:not(:last-child):after {
  border-top: 0;
  border-left: 1px solid #169db0;
}
.horizontal .org-tree-node:not(:only-child):after {
  border-top: 1px solid #169db0;
}
.horizontal .org-tree-node .org-tree-node-inner {
  display: table;
}
.horizontal .org-tree-node-label {
  display: table-cell;
  vertical-align: middle;
}
.horizontal.collapsable .org-tree-node.collapsed {
  padding-right: 30px;
}
.horizontal.collapsable .org-tree-node.collapsed .org-tree-node-label:after {
  top: 0;
  left: 100%;
  width: 20px;
  height: 50%;
  border-right: 0;
  border-bottom: 1px solid #169db0;
}
.horizontal .org-tree-node-btn {
  top: 50%;
  left: 100%;
  margin-top: -11px;
  margin-left: 19px;
}
.horizontal > .org-tree-node:only-child:before {
  border-bottom: 0;
}
.horizontal .org-tree-node-children {
  display: table-cell;
  padding-top: 0;
  padding-left: 30px;
}
.horizontal .org-tree-node-children:before {
  top: 50%;
  left: 0;
  width: 20px;
  height: 0;
  border-left: 0;
  border-top: 1px solid #169db0;
}
.horizontal .org-tree-node-children:after {
  display: none;
}
.horizontal .org-tree-node-children > .org-tree-node {
  display: block;
}
</style>